package com.oracle.dao;

import com.oracle.dao.impl.AdminDao;
import com.oracle.mapper.AdminInfoMapper;
import com.oracle.mapper.AdminMapper;
import com.oracle.pojo.Admin;
import com.oracle.pojo.AdminExample;
import com.oracle.pojo.AdminInfo;
import com.oracle.pojo.AdminInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminDaoImpl implements AdminDao {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Override
    public Admin selectAdminByUsername(Admin admin) {
        //通过后台用户名来查询字段
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());
        List<Admin> list = adminMapper.selectByExample(example);
        //如果没有查询到数据则返回null,如果查询到数据，则返回第一条数据
        Admin adminDo = list.isEmpty()? null :  list.get(0);
        if (admin==null){
            return null;
        }
        return adminDo;
    }

    @Override
    public AdminInfo selectAdminInfoByMid(Integer mid) {
        return adminInfoMapper.selectByPrimaryKey(mid);
    }

    @Override
    public String updateAdminInfo(AdminInfo adminInfo) {
        Integer code = adminInfoMapper.updateByPrimaryKeySelective(adminInfo);
        if (code>0){
            return "YES";
        }
        return "NO";
    }
}
